package com.qd.extcamclientv2;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * 지도위에 이동경로를 표시하기 위한 클래스
 *
 */
public class ExtOverlay extends Overlay {
	private Paint mPaint = null;
	private PlayerActivity mPlayer = null;
	private boolean mIsTouchDown = false;
	private Point mOldPoint = new Point();
	private Point mNewPoint = new Point();

	/**
	 * @param player
	 * @param mapView
	 */
	public ExtOverlay(PlayerActivity player) {
		mPlayer = player;

		mPaint = new Paint();
		mPaint.setStrokeWidth(5);
		mPaint.setColor(Color.RED);
		mPaint.setStyle(Paint.Style.STROKE);
	}

	@Override
	public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {
		switch (arg0.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mIsTouchDown = true;
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			if (mIsTouchDown) {
				mIsTouchDown = false;
				arg1.postInvalidate();
			}
			break;
		}

		return super.onTouchEvent(arg0, arg1);
	}

	public boolean isTouchDown() {
		return mIsTouchDown;
	}

	/* (non-Javadoc)
	 * @see com.google.android.maps.Overlay#draw(android.graphics.Canvas, com.google.android.maps.MapView, boolean)
	 * 화면에 이동경로를 그린다.
	 */
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);

		int count = mPlayer.getGpsIndex() + 1;
		if (0 < count) {
			mPlayer.getGpsPoint(0, mOldPoint);
			for (int i=0; i<count; i++) {
				mPlayer.getGpsPoint(i, mNewPoint);
				if (!(mOldPoint.x == -1 || mNewPoint.x == -1))
					canvas.drawLine(mOldPoint.x, mOldPoint.y, mNewPoint.x, mNewPoint.y, this.mPaint);
				mOldPoint.set(mNewPoint.x, mNewPoint.y);
			}
		}
	}
}
